package org.example.sword2offer.primary;
import java.util.Stack;

/**
 * @author: lynn
 * @Descript: 包含min函数的栈
 * 定义栈的数据结构，请在该类型中实现一个能够得到栈中所含最小元素的min函数（时间复杂度应为O（1））
 * @date: 2021/3/10 21:21
 * @version: 1.0
 */
public class Q20_MinStack {

    //stack 判空isEmpty , null
    public class Solution {
         Stack<Integer> dataStack=new Stack<>();
         Stack<Integer> minStack=new Stack<>();

        public void push(int node) {
            dataStack.push(node);
            if (minStack.isEmpty() || minStack.peek()>node){
              minStack.push(node);
            }
        }

        public void pop() {
            if (dataStack.peek()==minStack.peek()){
                dataStack.pop();
                minStack.pop();
            }else {
                dataStack.pop();
            }
        }
        //获取栈顶元素
        public int top() {
            if (!dataStack.empty()){
                return dataStack.peek();
            }else {
                throw new RuntimeException("data stack is empty !");
            }

        }

        public int min() {
            if (!minStack.empty()){
                return minStack.peek();
            }else {
                throw new RuntimeException("data stack is empty !");
            }
        }
    }

}
